﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shumisen.Core.Composition.Aop;

namespace Shumisen.Core.UnitTest.Composition.Aop
{
	[TestClass]
	public class AopProxyTest
	{
		[TestMethod]
		public void TestConstructor()
		{
			var builder = new StringBuilder();
			var proxy = new AopProxy(typeof(IHoge),
			                         new Hoge("1", builder),
			                         new List<IInterceptor>
			                         {
			                         	new MyInterceptor("2", builder),
			                         	new MyInterceptor("3", builder),
			                         	new MyInterceptor("4", builder)
			                         });
			var hoge = (IHoge) proxy.GetTransparentProxy();
			hoge.Foo();
			Assert.AreEqual("2341", builder.ToString());
		}

		public class MyInterceptor : IInterceptor
		{
			private readonly string _key;

			private readonly StringBuilder _builder;

			public MyInterceptor(String key, StringBuilder builder)
			{
				_key = key;
				_builder = builder;
			}

			public void Intercept(IInvocation invocation)
			{
				_builder.Append(_key);
				invocation.Proceed();
			}
		}

		public interface IHoge
		{
			void Foo();
		}

		public class Hoge : IHoge
		{
			private readonly string _key;

			private readonly StringBuilder _builder;

			public Hoge(string key, StringBuilder builder)
			{
				_key = key;
				_builder = builder;
			}

			public void Foo()
			{
				_builder.Append(_key);
			}
		}
	}
}